
package jp.moisture.othello.player;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import jp.moisture.othello.board.Cell;

public class HumanPlayer extends Player {

    public HumanPlayer(String name) {
        super(name);
    }

    public Cell next() {
        List<Cell> cells = mBoard.findPutableCells(mColor);
        if (cells.isEmpty()) {
            return null;
        } else if (cells.size() == 1) {
            return cells.get(0);
        }

        do {
            try {
                System.out.print("input cell : ");
                BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
                String line = r.readLine().trim();
                //                String[] words = line.split("[,\\s\\.]");
                if (line.length() != 2) {
                    continue;
                }

                int x = Integer.parseInt("" + line.charAt(0)) - 1;
                int y = Integer.parseInt("" + line.charAt(1)) - 1;
                for (Cell cell : cells) {
                    if (cell.getX() == x && cell.getY() == y) {
                        return cell;
                    }
                }
                System.err.println("not match.");
            } catch (Exception e) {
                System.err.println("error : " + e.getMessage());
            }

        } while (true);
    }
}
